/*
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2003-2008 Intel Corporation. All Rights Reserved.
//
//
*/
#include "umc_defs.h"
#if defined (UMC_ENABLE_DV_VIDEO_DECODER)

#ifndef __UMC_DV_INTERNAL_H
#define __UMC_DV_INTERNAL_H

#include "umc_dv_sequence.h"

namespace UMC
{

#pragma pack(1)

// See details in ippi manual, Function HuffmanDecodeSegment_DV
class BlockParam
{
public:
    Ipp32u     : 4;                                       // empty bits
    Ipp32u cl  : 2;                                       // quantization class of block
    Ipp32u m0  : 1;                                       // type of block
    Ipp32u     : 9;                                       // empty bits
    Ipp32u qno : 4;                                       // quantization number of block
    Ipp32u     : 12;                                      // empty bits
};

#pragma pack()

inline
Ipp8u SaturateByte(Ipp32s lValue)
{
    if (255 < lValue)
        return 255;
    else if (0 > lValue)
        return 0;

    return static_cast<Ipp8u> (lValue);

} // Ipp8u SaturateByte(Ipp32s lValue)

//Some need tables


#define _INTERNAL_CS1 0.980785  //cos(1*PI/16)
#define _INTERNAL_CS2 0.923880  //cos(2*PI/16)
#define _INTERNAL_CS3 0.831470  //cos(3*PI/16)
#define _INTERNAL_CS4 0.707107  //cos(4*PI/16)
#define _INTERNAL_CS5 0.555570  //cos(5*PI/16)
#define _INTERNAL_CS6 0.382683  //cos(6*PI/16)
#define _INTERNAL_CS7 0.195090  //cos(7*PI/16)

#define _W0 (1.0000001)
#define _W1 (_INTERNAL_CS4 / (4.0 * _INTERNAL_CS7 * _INTERNAL_CS2))
#define _W2 (_INTERNAL_CS4 / (2.0 * _INTERNAL_CS6))
#define _W3 (1.0 / (2.0 * _INTERNAL_CS5))
#define _W4 (7.0 / 8.0)
#define _W5 (_INTERNAL_CS4 / _INTERNAL_CS3)
#define _W6 (_INTERNAL_CS4 / _INTERNAL_CS2)
#define _W7 (_INTERNAL_CS4 / _INTERNAL_CS1)


static double TablW0[] =
    {
        (0.5/*2500076*/),(_W0*_W1/2),(_W0*_W2/2),(_W0*_W3/2),(_W0*_W4/2),(_W0*_W5/2),(_W0*_W6/2),(_W0*_W7/2),
            (_W1*_W0/2),(_W1*_W1/2),(_W1*_W2/2),(_W1*_W3/2),(_W1*_W4/2),(_W1*_W5/2),(_W1*_W6/2),(_W1*_W7/2),
            (_W2*_W0/2),(_W2*_W1/2),(_W2*_W2/2),(_W2*_W3/2),(_W2*_W4/2),(_W2*_W5/2),(_W2*_W6/2),(_W2*_W7/2),
            (_W3*_W0/2),(_W3*_W1/2),(_W3*_W2/2),(_W3*_W3/2),(_W3*_W4/2),(_W3*_W5/2),(_W3*_W6/2),(_W3*_W7/2),

            (_W4*_W0/2),(_W4*_W1/2),(_W4*_W2/2),(_W4*_W3/2),(_W4*_W4/2),(_W4*_W5/2),(_W4*_W6/2),(_W4*_W7/2),
            (_W5*_W0/2),(_W5*_W1/2),(_W5*_W2/2),(_W5*_W3/2),(_W5*_W4/2),(_W5*_W5/2),(_W5*_W6/2),(_W5*_W7/2),
            (_W6*_W0/2),(_W6*_W1/2),(_W6*_W2/2),(_W6*_W3/2),(_W6*_W4/2),(_W6*_W5/2),(_W6*_W6/2),(_W6*_W7/2),
            (_W7*_W0/2),(_W7*_W1/2),(_W7*_W2/2),(_W7*_W3/2),(_W7*_W4/2),(_W7*_W5/2),(_W7*_W6/2),(_W7*_W7/2)
    };

    static double TablW0_ZigZag[] =
    {
         TablW0[0],TablW0[1],TablW0[8],TablW0[16],     TablW0[9],TablW0[2],TablW0[3],TablW0[10],
         TablW0[17],TablW0[24],TablW0[32],TablW0[25],  TablW0[18],TablW0[11],TablW0[4],TablW0[5],
         TablW0[12],TablW0[19],TablW0[26],TablW0[33],  TablW0[40],TablW0[48],TablW0[41],TablW0[34],
         TablW0[27], TablW0[20],TablW0[13],TablW0[6],  TablW0[7],TablW0[14],TablW0[21],TablW0[28],

         TablW0[35], TablW0[42],TablW0[49],TablW0[56], TablW0[57],TablW0[50],TablW0[43],TablW0[36],
         TablW0[29], TablW0[22],TablW0[15],TablW0[23], TablW0[30],TablW0[37],TablW0[44],TablW0[51],
         TablW0[58], TablW0[59],TablW0[52],TablW0[45], TablW0[38],TablW0[31],TablW0[39],TablW0[46],
         TablW0[53], TablW0[60],TablW0[61],TablW0[54], TablW0[47],TablW0[55],TablW0[62],TablW0[63]
    };

    static Ipp16s TablW0_Scale_ZigZag[] =
    {
         (Ipp16s) (0.5 + 8191.0 /TablW0[0]), (Ipp16s) (0.5 + 8191.0 /TablW0[1]), (Ipp16s) (0.5 + 8191.0 /TablW0[8]), (Ipp16s) (0.5 + 8191.0 /TablW0[16]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[9]), (Ipp16s) (0.5 + 8191.0 /TablW0[2]), (Ipp16s) (0.5 + 8191.0 /TablW0[3]), (Ipp16s) (0.5 + 8191.0 /TablW0[10]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[17]),(Ipp16s) (0.5 + 8191.0 /TablW0[24]),(Ipp16s) (0.5 + 8191.0 /TablW0[32]),(Ipp16s) (0.5 + 8191.0 /TablW0[25]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[18]),(Ipp16s) (0.5 + 8191.0 /TablW0[11]),(Ipp16s) (0.5 + 8191.0 /TablW0[4]), (Ipp16s) (0.5 + 8191.0 /TablW0[5]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[12]),(Ipp16s) (0.5 + 8191.0 /TablW0[19]),(Ipp16s) (0.5 + 8191.0 /TablW0[26]),(Ipp16s) (0.5 + 8191.0 /TablW0[33]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[40]),(Ipp16s) (0.5 + 8191.0 /TablW0[48]),(Ipp16s) (0.5 + 8191.0 /TablW0[41]),(Ipp16s) (0.5 + 8191.0 /TablW0[34]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[27]),(Ipp16s) (0.5 + 8191.0 /TablW0[20]),(Ipp16s) (0.5 + 8191.0 /TablW0[13]),(Ipp16s) (0.5 + 8191.0 /TablW0[6]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[7]), (Ipp16s) (0.5 + 8191.0 /TablW0[14]),(Ipp16s) (0.5 + 8191.0 /TablW0[21]),(Ipp16s) (0.5 + 8191.0 /TablW0[28]),

         (Ipp16s) (0.5 + 8191.0 /TablW0[35]),(Ipp16s) (0.5 + 8191.0 /TablW0[42]), (Ipp16s) (0.5 + 8191.0 /TablW0[49]),(Ipp16s) (0.5 + 8191.0 /TablW0[56]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[57]),(Ipp16s) (0.5 + 8191.0 /TablW0[50]), (Ipp16s) (0.5 + 8191.0 /TablW0[43]),(Ipp16s) (0.5 + 8191.0 /TablW0[36]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[29]),(Ipp16s) (0.5 + 8191.0 / TablW0[22]),(Ipp16s) (0.5 + 8191.0 /TablW0[15]),(Ipp16s) (0.5 + 8191.0 /TablW0[23]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[30]),(Ipp16s) (0.5 + 8191.0 /TablW0[37]), (Ipp16s) (0.5 + 8191.0 /TablW0[44]),(Ipp16s) (0.5 + 8191.0 /TablW0[51]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[58]),(Ipp16s) (0.5 + 8191.0 / TablW0[59]),(Ipp16s) (0.5 + 8191.0 /TablW0[52]),(Ipp16s) (0.5 + 8191.0 /TablW0[45]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[38]),(Ipp16s) (0.5 + 8191.0 /TablW0[31]), (Ipp16s) (0.5 + 8191.0 /TablW0[39]),(Ipp16s) (0.5 + 8191.0 /TablW0[46]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[53]),(Ipp16s) (0.5 + 8191.0 / TablW0[60]),(Ipp16s) (0.5 + 8191.0 /TablW0[61]),(Ipp16s) (0.5 + 8191.0 /TablW0[54]),
         (Ipp16s) (0.5 + 8191.0 /TablW0[47]),(Ipp16s) (0.5 + 8191.0 /TablW0[55]), (Ipp16s) (0.5 + 8191.0 /TablW0[62]),(Ipp16s) (0.5 + 8191.0 /TablW0[63])
    };

    static double TablW1[] =
    {
        (0.5/*2500076*/),(_W0*_W1/2),(_W0*_W2/2),(_W0*_W3/2),(_W0*_W4/2),(_W0*_W5/2),(_W0*_W6/2),(_W0*_W7/2),
            (_W2*_W0/2),(_W2*_W1/2),(_W2*_W2/2),(_W2*_W3/2),(_W2*_W4/2),(_W2*_W5/2),(_W2*_W6/2),(_W2*_W7/2),
            (_W4*_W0/2),(_W4*_W1/2),(_W4*_W2/2),(_W4*_W3/2),(_W4*_W4/2),(_W4*_W5/2),(_W4*_W6/2),(_W4*_W7/2),
            (_W6*_W0/2),(_W6*_W1/2),(_W6*_W2/2),(_W6*_W3/2),(_W6*_W4/2),(_W6*_W5/2),(_W6*_W6/2),(_W6*_W7/2),
            (_W0*_W0/2),(_W0*_W1/2),(_W0*_W2/2),(_W0*_W3/2),(_W0*_W4/2),(_W0*_W5/2),(_W0*_W6/2),(_W0*_W7/2),
            (_W2*_W0/2),(_W2*_W1/2),(_W2*_W2/2),(_W2*_W3/2),(_W2*_W4/2),(_W2*_W5/2),(_W2*_W6/2),(_W2*_W7/2),
            (_W4*_W0/2),(_W4*_W1/2),(_W4*_W2/2),(_W4*_W3/2),(_W4*_W4/2),(_W4*_W5/2),(_W4*_W6/2),(_W4*_W7/2),
            (_W6*_W0/2),(_W6*_W1/2),(_W6*_W2/2),(_W6*_W3/2),(_W6*_W4/2),(_W6*_W5/2),(_W6*_W6/2),(_W6*_W7/2),
    };

    static double TablW1_ZigZag[] =
    {
        TablW1[0],TablW1[32],TablW1[1],TablW1[33], TablW1[8],TablW1[40],TablW1[2],TablW1[34],
        TablW1[9],TablW1[41],TablW1[16],TablW1[48], TablW1[24],TablW1[56],TablW1[17],TablW1[49],
        TablW1[10],TablW1[42],TablW1[3],TablW1[35], TablW1[4],TablW1[36],TablW1[11],TablW1[43],
        TablW1[18],TablW1[50],TablW1[25],TablW1[57], TablW1[26],TablW1[58],TablW1[19],TablW1[51],

        TablW1[12],TablW1[44],TablW1[5],TablW1[37], TablW1[6],TablW1[38],TablW1[13],TablW1[45],
        TablW1[20],TablW1[52],TablW1[27],TablW1[59], TablW1[28],TablW1[60],TablW1[21],TablW1[53],
        TablW1[14],TablW1[46],TablW1[7],TablW1[39], TablW1[15],TablW1[47],TablW1[22],TablW1[54],
        TablW1[29],TablW1[61],TablW1[30],TablW1[62], TablW1[23],TablW1[55],TablW1[31],TablW1[63]
    };

    static Ipp16s TablW1_Scale_ZigZag[] =
    {
        (Ipp16s) (0.5 + 8191.0 /TablW1[0]), (Ipp16s) (0.5 + 8191.0 /TablW1[32]),(Ipp16s) (0.5 + 8191.0 /TablW1[1]), (Ipp16s) (0.5 + 8191.0 /TablW1[33]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[8]), (Ipp16s) (0.5 + 8191.0 /TablW1[40]),(Ipp16s) (0.5 + 8191.0 /TablW1[2]), (Ipp16s) (0.5 + 8191.0 /TablW1[34]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[9]), (Ipp16s) (0.5 + 8191.0 /TablW1[41]),(Ipp16s) (0.5 + 8191.0 /TablW1[16]),(Ipp16s) (0.5 + 8191.0 /TablW1[48]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[24]),(Ipp16s) (0.5 + 8191.0 /TablW1[56]),(Ipp16s) (0.5 + 8191.0 /TablW1[17]),(Ipp16s) (0.5 + 8191.0 /TablW1[49]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[10]),(Ipp16s) (0.5 + 8191.0 /TablW1[42]),(Ipp16s) (0.5 + 8191.0 /TablW1[3]), (Ipp16s) (0.5 + 8191.0 /TablW1[35]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[4]), (Ipp16s) (0.5 + 8191.0 /TablW1[36]),(Ipp16s) (0.5 + 8191.0 /TablW1[11]),(Ipp16s) (0.5 + 8191.0 /TablW1[43]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[18]),(Ipp16s) (0.5 + 8191.0 /TablW1[50]),(Ipp16s) (0.5 + 8191.0 /TablW1[25]),(Ipp16s) (0.5 + 8191.0 /TablW1[57]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[26]),(Ipp16s) (0.5 + 8191.0 /TablW1[58]),(Ipp16s) (0.5 + 8191.0 /TablW1[19]),(Ipp16s) (0.5 + 8191.0 /TablW1[51]),

        (Ipp16s) (0.5 + 8191.0 /TablW1[12]),(Ipp16s) (0.5 + 8191.0 /TablW1[44]),(Ipp16s) (0.5 + 8191.0 /TablW1[5]), (Ipp16s) (0.5 + 8191.0 /TablW1[37]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[6]), (Ipp16s) (0.5 + 8191.0 /TablW1[38]),(Ipp16s) (0.5 + 8191.0 /TablW1[13]),(Ipp16s) (0.5 + 8191.0 /TablW1[45]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[20]),(Ipp16s) (0.5 + 8191.0 /TablW1[52]),(Ipp16s) (0.5 + 8191.0 /TablW1[27]),(Ipp16s) (0.5 + 8191.0 /TablW1[59]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[28]),(Ipp16s) (0.5 + 8191.0 /TablW1[60]),(Ipp16s) (0.5 + 8191.0 /TablW1[21]),(Ipp16s) (0.5 + 8191.0 /TablW1[53]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[14]),(Ipp16s) (0.5 + 8191.0 /TablW1[46]),(Ipp16s) (0.5 + 8191.0 /TablW1[7]), (Ipp16s) (0.5 + 8191.0 /TablW1[39]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[15]),(Ipp16s) (0.5 + 8191.0 /TablW1[47]),(Ipp16s) (0.5 + 8191.0 /TablW1[22]),(Ipp16s) (0.5 + 8191.0 /TablW1[54]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[29]),(Ipp16s) (0.5 + 8191.0 /TablW1[61]),(Ipp16s) (0.5 + 8191.0 /TablW1[30]),(Ipp16s) (0.5 + 8191.0 /TablW1[62]),
        (Ipp16s) (0.5 + 8191.0 /TablW1[23]),(Ipp16s) (0.5 + 8191.0 /TablW1[55]),(Ipp16s) (0.5 + 8191.0 /TablW1[31]),(Ipp16s) (0.5 + 8191.0 /TablW1[63])
    };

#undef _INTERNAL_CS1
#undef _INTERNAL_CS2
#undef _INTERNAL_CS3
#undef _INTERNAL_CS4
#undef _INTERNAL_CS5
#undef _INTERNAL_CS6
#undef _INTERNAL_CS7

#undef _W0
#undef _W1
#undef _W2
#undef _W3
#undef _W4
#undef _W5
#undef _W6
#undef _W7

} // end namespace UMC

#endif // __UMC_DV_INTERNAL_H

#endif //(UMC_ENABLE_DV_VIDEO_DECODER)
